package com.nf.health.app.models;

import com.google.gson.Gson;
import com.nf.health.app.core.MyGsonBuilder;
import com.nf.health.app.utils.BigDecimalUtils;
import com.nf.health.app.utils.DateUtil;
import com.nf.health.app.utils.LogUtil;
import com.nf.health.app.utils.PreferenceHelper;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeMap;

import cn.bluetools.SleepsData;

/**
 * Created by Jason on 2016/6/17.
 */
public class SubSleepTemp implements Serializable {


    /**
     * quality : 1
     * sleepDuration : 1
     * awakeDuration : 1
     * lightDuration : 1
     * eepDuration : 1
     * totalDuration : 1
     * awakeCount : 1
     */

    private String quality;
    private String sleepDuration;
    private String awakeDuration;
    private String lightDuration;
    private String deepDuration;
    private String totalDuration;
    private String awakeCount;
    private List<SleepsData> longTimeList;
    private String timeStamp;
    public String getQuality() {
        return quality;
    }

    public void setQuality(String quality) {
        this.quality = quality;
    }

    public String getSleepDuration() {
        return sleepDuration;
    }

    public void setSleepDuration(String sleepDuration) {
        this.sleepDuration = sleepDuration;
    }

    public String getAwakeDuration() {
        return awakeDuration;
    }

    public void setAwakeDuration(String awakeDuration) {
        this.awakeDuration = awakeDuration;
    }

    public String getLightDuration() {
        return lightDuration;
    }

    public void setLightDuration(String lightDuration) {
        this.lightDuration = lightDuration;
    }

    public String getDeepDuration() {
        return deepDuration;
    }

    public void setDeepDuration(String eepDuration) {
        this.deepDuration = eepDuration;
    }

    public String getTotalDuration() {
        return totalDuration;
    }

    public void setTotalDuration(String totalDuration) {
        this.totalDuration = totalDuration;
    }

    public String getAwakeCount() {
        return awakeCount;
    }

    public void setAwakeCount(String awakeCount) {
        this.awakeCount = awakeCount;
    }

    public List<SleepsData> getLongTimeList() {
        return longTimeList;
    }

    public void setLongTimeList(List<SleepsData> longTimeList) {
        this.longTimeList = longTimeList;
    }



    /*public SubSleepTemp getSubSleepTemp(String [] str){
        SubSleepTemp subSleepTemp = new SubSleepTemp();

        return subSleepTemp;
    }*/

    public String getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(String timeStamp) {
        this.timeStamp = timeStamp;
    }

    @Override
    public String toString() {
        return "SubSleepTemp{" +
                "quality='" + quality + '\'' +
                ", sleepDuration='" + sleepDuration + '\'' +
                ", awakeDuration='" + awakeDuration + '\'' +
                ", lightDuration='" + lightDuration + '\'' +
                ", eepDuration='" + deepDuration + '\'' +
                ", totalDuration='" + totalDuration + '\'' +
                ", awakeCount='" + awakeCount + '\'' +
                ", longTimeList=" + longTimeList +
                ", timeStamp='" + timeStamp + '\'' +
                '}';
    }
    public SubSleepTemp getSubSleepTemp(List<SleepsData> longTimeList){
        SubSleepTemp subSleepTemp = new SubSleepTemp();
        long lightDuration = 0 ;
        long deepDuration = 0 ;
        long wakeDuration = 0 ;
        int awakeCOunt = 0 ;//清醒次数
        int quality = 100;//睡眠质量
        long totalDuration ;//睡眠总时间
        long sleepDuration = 0 ;//睡眠时间
        long timeStamp = longTimeList.size()>0 ? longTimeList.get(0).time_stamp : 0 ;
        for (int i = 1; i < longTimeList.size(); i++) {
            SleepsData model = longTimeList.get(i);
            int status = model.status;
            long time_stamp = model.time_stamp;
            long time_stamp_next = model.time_stamp;
            /*if(i<longTimeList.size() -1){
                time_stamp_next =  longTimeList.get(i+1).time_stamp;
            }
            if (time_stamp == 2){//2: 浅睡-

                lightDuration += (time_stamp_next - time_stamp);
            }
            else if (longTimeList.get(i).status == 1){//1:睡着--
                deepDuration += (time_stamp_next - time_stamp);

            }
            else if (longTimeList.get(i).status == 4){ //4:准备入睡
                wakeDuration += (time_stamp_next - time_stamp);
                awakeCOunt++ ;
            }  else if (longTimeList.get(i).status == 3){//3--醒着
                wakeDuration += (time_stamp_next - time_stamp);
                quality-- ;
            }*/

            /*** 0 深睡  1 浅睡  2 清醒  3 入睡  ***/
            if (longTimeList.get(i).status == 2){//2: 浅睡-
                sleepDuration += (longTimeList.get(i).time_stamp - longTimeList.get((i-1)).time_stamp);
                lightDuration += (longTimeList.get(i).time_stamp - longTimeList.get((i-1)).time_stamp);
            }
            else if (longTimeList.get(i).status == 1){//1:睡着--
                deepDuration += (longTimeList.get(i).time_stamp - longTimeList.get(i-1).time_stamp);
                sleepDuration += (longTimeList.get(i).time_stamp - longTimeList.get((i-1)).time_stamp);
            }
            else if (longTimeList.get(i).status == 4){ //4:准备入睡
                wakeDuration += (longTimeList.get(i).time_stamp - longTimeList.get(i-1).time_stamp);
                sleepDuration += (longTimeList.get(i).time_stamp - longTimeList.get((i-1)).time_stamp);
                awakeCOunt++ ;
                quality-- ;
            }  else if (longTimeList.get(i).status == 3){//3--醒着
                wakeDuration += (longTimeList.get(i).time_stamp - longTimeList.get(i-1).time_stamp);
                sleepDuration += (longTimeList.get(i).time_stamp - longTimeList.get((i-1)).time_stamp);
                quality-- ;
            }

        }
        totalDuration = lightDuration + deepDuration + wakeDuration ;

        subSleepTemp.setTimeStamp(String.valueOf(timeStamp));
        subSleepTemp.setAwakeCount(String.valueOf(awakeCOunt));
        subSleepTemp.setAwakeDuration(String.valueOf(wakeDuration));
        subSleepTemp.setDeepDuration(String.valueOf(deepDuration));
        subSleepTemp.setLightDuration(String.valueOf(lightDuration));
        subSleepTemp.setQuality(String.valueOf(quality));
        subSleepTemp.setSleepDuration(String.valueOf(totalDuration));
        subSleepTemp.setTotalDuration(String.valueOf(totalDuration));

        return subSleepTemp;
    }

    public SubSleepTemp getSubSleepTemp2(List<SleepsData> longTimeList){
        SubSleepTemp subSleepTemp = new SubSleepTemp();
        long lightDuration = 0 ;//浅睡时间
        long deepDuration = 0 ;//深睡时间
        long wakeDuration = 0 ;//清醒时间
        int awakeCOunt = 0 ;//清醒次数
        int quality = 100;//睡眠质量
        long totalDuration ;//睡眠总时间
        long sleepDuration = 0 ;//睡眠时间
        long timeStamp = 0l;//时间点
        if(longTimeList.size()>0){
            timeStamp = longTimeList.get(0).time_stamp;
        }


        for (int i = 1; i < longTimeList.size(); i++) {//起始索引为1，跳过16的计算
            SleepsData curretnMextModel = longTimeList.get(i);
            SleepsData previousModel = null;
            long period = 0;//每一段的秒数
            if (i < longTimeList.size()) {
                previousModel = longTimeList.get(i - 1);
                period =  curretnMextModel.time_stamp - previousModel.time_stamp; // D_MINUTE;//计算每一段的秒数
            }
            /*** 0 深睡  1 浅睡  2 清醒  3 入睡  ***/
            if (curretnMextModel.status == 1) {//1:睡着--深睡
                deepDuration += period;
                sleepDuration += period;
            } else if (curretnMextModel.status == 2) {//2: 浅睡-
                lightDuration += period;
                sleepDuration += period;
            } else if (curretnMextModel.status == 4) { //4:准备入睡
                sleepDuration += period;
                wakeDuration += period;//清醒时间=入睡时间+清醒时间
            } else if (curretnMextModel.status == 3) {//3--醒着
                wakeDuration += period;
                sleepDuration += period;
                awakeCOunt++;//先算总的清醒次数
                wakeDuration += (longTimeList.get(i).time_stamp - longTimeList.get(i - 1).time_stamp);
                quality--;
            }
        }
        totalDuration =   longTimeList.get(longTimeList.size() - 1).time_stamp - longTimeList.get(1).time_stamp;
       // totalDuration = lightDuration + deepDuration + wakeDuration ;
        subSleepTemp.setTimeStamp(String.valueOf(timeStamp));
        subSleepTemp.setAwakeCount(String.valueOf(awakeCOunt));
        subSleepTemp.setAwakeDuration(String.valueOf(wakeDuration));
        subSleepTemp.setDeepDuration(String.valueOf(deepDuration));
        subSleepTemp.setLightDuration(String.valueOf(lightDuration));
        subSleepTemp.setQuality(String.valueOf(quality));
        subSleepTemp.setSleepDuration(String.valueOf(sleepDuration));
        subSleepTemp.setTotalDuration(String.valueOf(sleepDuration));

        return subSleepTemp;
    }

    /**读取睡眠历史数据，去除最近的睡眠时间***/
    public String getSleepHistoryOneDayDate(List<SleepsData> sleepsDatas){


        if(sleepsDatas != null){
            for(int i = 0 ; i < sleepsDatas.size(); i++){
                LogUtil.e("yuyahao", DateUtil.date2String(sleepsDatas.get(i).time_stamp * 1000, "yyyy-MM-dd HH:mm:ss") + "----->" + sleepsDatas.get(i).status + "--->" + sleepsDatas.get(i).time_stamp);
            }
        }
        String resultSleepData = "0.0";
        if(sleepsDatas == null){
            return resultSleepData;
        }
        LinkedList<List<SleepsData>> linkList =  getSleepHistoryDataDetailList(sleepsDatas);
        for (int i = linkList.size() - 1; i >= 0; i--) {
            SubSleepTemp subSleepTemp = new SubSleepTemp().getSubSleepTemp2(linkList.get(i));
            //获取最后一天的睡眠数据
                    //date2String(System.currentTimeMillis(),"yyyy-MM-dd HH:mm:ss")+"   "+System.currentTimeMillis()
                    String ss1 =  DateUtil.date2String(Long.parseLong(subSleepTemp.getTimeStamp()) *1000L, "yyyy-MM-dd HH:mm:ss");
                    String ss2 =  DateUtil.date2String(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss");
           /* if(DateUtil.isSameDayOfMillis(Long.parseLong(subSleepTemp.getTimeStamp()) * 1000L, System.currentTimeMillis())){
            }else{*/
            if(i == (linkList.size() - 1 )){

                resultSleepData =   "" + BigDecimalUtils.divide2(Long.parseLong(subSleepTemp.getSleepDuration()), 3600).toString();
                break;
            }
//                break;
//            }
        }
        return resultSleepData;
    }
    /**读取睡眠历史数据，返回一个HashMap集合（分离天数）***/
    public HashMap<String, List<SleepsData>>  getSleepHistoryDataDetail(List<SleepsData> sleepsDatas){
        HashMap<String, List<SleepsData>> dataMapList = new HashMap<>();
        List<SleepsData> subTempList = new ArrayList();
        boolean isStart = false;
        try {
            for (int i = 0; i < sleepsDatas.size(); i++) {
                if (sleepsDatas.get(i).status == 6) {
                    isStart = true;
                    continue;
                }

                if (sleepsDatas.get(i).status == 7 && sleepsDatas.get(i-1).status != 7) {
                    List<SleepsData> tempList = new ArrayList();
                    tempList.addAll(subTempList);
                    isStart = false;
                    dataMapList.put(String.valueOf(sleepsDatas.get(i).time_stamp), tempList);
                    subTempList.clear();
                    continue;
                }
                if (isStart) {
                    subTempList.add(sleepsDatas.get(i));
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataMapList;
    }
    /**读取睡眠历史数据，返回一个HashMap集合（分离天数）***/
    public LinkedList<List<SleepsData>> getSleepHistoryDataDetailList(List<SleepsData> sleepsDatas){
        LinkedList<List<SleepsData>> dataMapList = new LinkedList<>();
        List<SleepsData> subTempList = new ArrayList();
        boolean isStart = false;
        try {
            for (int i = 0; i < sleepsDatas.size(); i++) {
                if (sleepsDatas.get(i).status == 6) {
                    isStart = true;
                    continue;
                }
                if (sleepsDatas.get(i).status == 7  && sleepsDatas.get(i-1).status != 7) {
                    List<SleepsData> tempList = new ArrayList();
                    tempList.addAll(subTempList);
                    isStart = false;
                    dataMapList.add(tempList);
                    subTempList.clear();
                    continue;
                }
                if (isStart) {
                    subTempList.add(sleepsDatas.get(i));
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataMapList;
    }

    /**List封装JSONArray***/
    public JSONArray getSleepHistoryJSONArray( HashMap<Long, List<List<SleepsData>>> data){
        JSONArray jsonArray = new JSONArray();
        try {
            for (HashMap.Entry<Long,  List<List<SleepsData>>> entry : data.entrySet()) {
                List<List<SleepsData>> list = entry.getValue();
                if(list == null){
                    continue;
                }
                long lightDuration = 0 ;//浅睡时间
                long deepDuration = 0 ;//深睡时间
                long wakeDuration = 0 ;//清醒时间
                int awakeCount = 0 ;//清醒次数
                int quality = 100;//睡眠质量
                long totalDuration = 0;//睡眠总时间
                long sleepDuration = 0 ;//睡眠时间
                long timeStamp = 0l;//时间点
               // JSONArray jsonotimeAndStatus = new JSONArray();//detail; 时间撮，状态
//                JSONObject jsonotimeAndStatus = new JSONObject();
                HashMap<String ,Integer> jsonotimeAndStatus= new HashMap<>();
//                List<SleepsData> jsonotimeAndStatus = new ArrayList();
                for (int i = 0; i <list.size(); i++) {
                    if(list.size()>0){
                        timeStamp = list.get(0).get(0).time_stamp;
                    }
                    List<SleepsData> perDataList = list.get(i);
                    for (int  j= 0; j < perDataList.size(); j++) {
                        SleepsData curretnMextModel = perDataList.get(j);
                        SleepsData previousModel = perDataList.get(j);
                        long period = 0L;//每一段的秒数
                        if (j < perDataList.size()-1) {
                            previousModel = perDataList.get(j + 1);
                            period =  previousModel.time_stamp - curretnMextModel.time_stamp; // D_MINUTE;//计算每一段的秒数
                        }
                        /*** 0 深睡  1 浅睡  2 清醒  3 入睡  ***/
                        if (curretnMextModel.status == 1) {//1:睡着--深睡
                            deepDuration += period;
                            sleepDuration += period;
                        } else if (curretnMextModel.status == 2) {//2: 浅睡-
                            lightDuration += period;
                            sleepDuration += period;
                        } else if (curretnMextModel.status == 4) { //4:准备入睡
                            sleepDuration += period;
                            wakeDuration += period;//清醒时间=入睡时间+清醒时间
                        } else if (curretnMextModel.status == 3) {//3--醒着
                            wakeDuration += period;
                            sleepDuration += period;
                            awakeCount++;//先算总的清醒次数
                            quality--;
                        }
                       /* SleepsData sleepsData = new SleepsData();
                        sleepsData.time_stamp = curretnMextModel.time_stamp;
                        sleepsData.status = curretnMextModel.status;
                        jsonotimeAndStatus.add(sleepsData);*/
                        jsonotimeAndStatus.put(String.valueOf(curretnMextModel.time_stamp),curretnMextModel.status);
                    }
                }
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("timeStamp", new StringBuilder(timeStamp+"").append("000").toString());
                JSONObject jsonObjectTimeStamp = new JSONObject();
                jsonObjectTimeStamp.put("quality", quality);
                jsonObjectTimeStamp.put("sleepDuration", DateUtil.getMinuteInt(sleepDuration));
                jsonObjectTimeStamp.put("awakeDuration", DateUtil.getMinuteInt(wakeDuration));
                jsonObjectTimeStamp.put("awakeCount", awakeCount);
                jsonObjectTimeStamp.put("lightDuration", DateUtil.getMinuteInt(lightDuration));
                jsonObjectTimeStamp.put("deepDuration", DateUtil.getMinuteInt(deepDuration));
                jsonObjectTimeStamp.put("totalDuration", DateUtil.getMinuteInt(sleepDuration));
                //jsonObjectTimeStamp.put("detail", JSONObject.toJSONString(answerMap));
                jsonObjectTimeStamp.put("detail", new JSONObject(new MyGsonBuilder().createGson().toJson(jsonotimeAndStatus)));
                jsonObject.put("sleep", jsonObjectTimeStamp);
                jsonArray.put(jsonObject);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
        }

        return jsonArray;
    }
    public JSONArray getSleepHistoryJSONArray2( TreeMap<Long, List<List<SleepsData>>> data){
        JSONArray jsonArray = new JSONArray();
        try {
            int indexList = 0 ;
            for (HashMap.Entry<Long,  List<List<SleepsData>>> entry : data.entrySet()) {
                List<List<SleepsData>> list = entry.getValue();
                if(list == null){
                    continue;
                }
                long lightDuration = 0 ;//浅睡时间
                long deepDuration = 0 ;//深睡时间
                long wakeDuration = 0 ;//清醒时间
                int awakeCount = 0 ;//清醒次数
                int quality = 100;//睡眠质量
                long totalDuration = 0;//睡眠总时间
                long sleepDuration = 0 ;//睡眠时间
                long timeStamp = entry.getKey();//时间点
                HashMap<String ,Integer> jsonotimeAndStatus= new HashMap<>();

                for (int i = 0; i <list.size(); i++) {
                   /* if(list.size()>0){
                        timeStamp = list.get(0).get(0).time_stamp;
                    }*/
                    List<SleepsData> perDataList = list.get(i);
                    for (int  j= 0; j < perDataList.size(); j++) {
                        SleepsData curretnMextModel = perDataList.get(j);
                        SleepsData previousModel = perDataList.get(j);
                        long period = 0L;//每一段的秒数
                        if (j < perDataList.size()-1) {
                            previousModel = perDataList.get(j + 1);
                            period =  previousModel.time_stamp - curretnMextModel.time_stamp; // D_MINUTE;//计算每一段的秒数
                        }
                        /*** 0 深睡  1 浅睡  2 清醒  3 入睡  ***/
                        if (curretnMextModel.status == 1) {//1:睡着--深睡
                            deepDuration += period;
                            //sleepDuration += period;
                        } else if (curretnMextModel.status == 2) {//2: 浅睡-
                            lightDuration += period;
                           // sleepDuration += period;
                        } else if (curretnMextModel.status == 4) { //4:准备入睡
                           // sleepDuration += period;
                            wakeDuration += period;//清醒时间=入睡时间+清醒时间
                        } else if (curretnMextModel.status == 3) {//3--醒着
                            wakeDuration += period;
                            //sleepDuration += period;
                            awakeCount++;//先算总的清醒次数
                            quality--;
                        }
                       /* SleepsData sleepsData = new SleepsData();
                        sleepsData.time_stamp = curretnMextModel.time_stamp;
                        sleepsData.status = curretnMextModel.status;
                        jsonotimeAndStatus.add(sleepsData);*/
                        jsonotimeAndStatus.put(String.valueOf(curretnMextModel.time_stamp),curretnMextModel.status);
                    }
                    //睡眠时间 =  结束时间- 开始时间
                    sleepDuration = sleepDuration + perDataList.get(perDataList.size() -1).time_stamp -  perDataList.get(0).time_stamp;
                }
                if(indexList == data.size() - 1){
                    int minute = BigDecimalUtils.getMathRound(sleepDuration,60f);
                    String sleepTotle =   "" + BigDecimalUtils.divide2(minute, 60).toString();
                    PreferenceHelper.putString("currentSleepData", sleepTotle);
                }
                indexList ++;
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("timeStamp", new StringBuilder(timeStamp+"").append("000").toString());
                JSONObject jsonObjectTimeStamp = new JSONObject();
                jsonObjectTimeStamp.put("quality", quality);
                jsonObjectTimeStamp.put("sleepDuration", DateUtil.getMinuteInt2(sleepDuration));
                jsonObjectTimeStamp.put("awakeDuration", DateUtil.getMinuteInt2(wakeDuration));
                jsonObjectTimeStamp.put("awakeCount", awakeCount);
                jsonObjectTimeStamp.put("lightDuration", DateUtil.getMinuteInt2(lightDuration));
                jsonObjectTimeStamp.put("deepDuration", DateUtil.getMinuteInt2(deepDuration));
                jsonObjectTimeStamp.put("totalDuration", DateUtil.getMinuteInt2(sleepDuration));
                //jsonObjectTimeStamp.put("detail", JSONObject.toJSONString(answerMap));
                jsonObjectTimeStamp.put("detail", new JSONObject(new MyGsonBuilder().createGson().toJson(jsonotimeAndStatus)));
                jsonObject.put("sleep", jsonObjectTimeStamp);
                jsonArray.put(jsonObject);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
        }
        return jsonArray;
    }
}
/*HashMap<String, List<SleepsData>> dataMapList = new SubSleepTemp().getSleepHistoryDataDetail(sleepsDatas);
        JSONArray jsonArray = new JSONArray();
        try {
            int isLast = 0;
            for (HashMap.Entry<String, List<SleepsData>> entry : dataMapList.entrySet()) {
                // String longTime = entry.getKey();//获取时间：年月日
                List<SleepsData> list = entry.getValue();//获取集合

                SubSleepTemp subSleepTemp = new SubSleepTemp().getSubSleepTemp2(list);
                //map.put(longTime,subSleepTemp);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("timeStamp", new StringBuilder(subSleepTemp.getTimeStamp()).append("000").toString());
                JSONObject jsonObjectTimeStamp = new JSONObject();

                jsonObjectTimeStamp.put("quality", subSleepTemp.getQuality());
                jsonObjectTimeStamp.put("sleepDuration", DateUtil.getMinute(subSleepTemp.getSleepDuration()));
                jsonObjectTimeStamp.put("awakeDuration", DateUtil.getMinute(subSleepTemp.getAwakeDuration()));
                jsonObjectTimeStamp.put("awakeCount", subSleepTemp.getAwakeCount());
                jsonObjectTimeStamp.put("lightDuration", DateUtil.getMinute(subSleepTemp.getLightDuration()));
                jsonObjectTimeStamp.put("deepDuration", DateUtil.getMinute(subSleepTemp.getDeepDuration()));
                jsonObjectTimeStamp.put("totalDuration", DateUtil.getMinute(subSleepTemp.getTotalDuration()));

                JSONObject subJsonObject = new JSONObject();
                for (int i = 0; i < list.size(); i++) {
                    subJsonObject.put(String.valueOf(list.get(i).time_stamp), list.get(i).status-1);
                }
                jsonObjectTimeStamp.put("detail", subJsonObject);
                jsonObject.put("sleep", jsonObjectTimeStamp);
                jsonArray.put(jsonObject);

                //获取最后一天的睡眠数据
                if(isLast == (dataMapList.size() - 1)){
                    sleep =   "" + BigDecimalUtils.divide2(Long.parseLong(subSleepTemp.getSleepDuration()), 3600).toString();
                    PreferenceHelper.putString("currentSleepData", sleep);
                    tv_tab_sleep.setText(sleep);
                }
                isLast++;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }*/

/************************保留了一份ios以后作参考*********************/
//-(void)calcSleepTotal{
//        for (NSArray *arr in self.sleepPeriodsArray) {
//        int inSleepTime = 0;
//        int soberTime = 0;
//        int lightSleepTime = 0;
//        int deepSleepTime =0;
//        int wakeups = 0;
//        int endSleepTime = 0;
//        for (int i = 1; i < arr.count ; i++) {//起始索引为1，跳过16的计算
//        DBSleepDetailData *currentDetail = (DBSleepDetailData *)arr[i];
//        DBSleepDetailData *nextDetail = nil;
//        int period = 0;
//        if (i < arr.count - 1) {
//        nextDetail = (DBSleepDetailData *)arr[i+1];
//        period =(int)(nextDetail.timeStamp - currentDetail.timeStamp) / D_MINUTE;//计算每一段的分钟
//        }
//
//        if (currentDetail.status == 0) {//深睡
//        deepSleepTime += period;
//        }else if (currentDetail.status == 1){//浅睡
//        lightSleepTime += period;
//        }else if(currentDetail.status == 3){//入睡
//        inSleepTime += period;
//        soberTime += period;//清醒时间=入睡时间+清醒时间
//        }else if(currentDetail.status == 2){//清醒
//        soberTime +=period;
//        wakeups ++;//先算总的清醒次数
//        }else if(currentDetail.status == 17){//结束时间
//        endSleepTime = (int)currentDetail.timeStamp;
//        int j = i;
//        while(--j){//再减掉不符合条件的清醒
//        DBSleepDetailData *pre = (DBSleepDetailData *)arr[j];
//        if (pre.status == 2) {
//        wakeups--;
//        }else{
//        break;
//        }
//        }
//        }else if(currentDetail.status == 18){//预设睡眠时间
//        int timeStamp = (int)currentDetail.timeStamp;
//        NSData *yearData = [NSData dataWithBytes: &timeStamp length: sizeof(timeStamp)];
//        Byte * yearByte = (Byte *)[yearData bytes];
//        int startHour = yearByte[0];
//        int startMinute = yearByte[1];
//        int endHour = yearByte[2];
//        int endMinute = yearByte[3];
//        int presetStartMins = startHour * 60 + startMinute;
//        int presetEndMins = endHour * 60 + endMinute;
//        //不跨天，睡眠所在的日期就是结束时间；跨天特殊处理，将没有超过24点算在第二天
//        if (presetStartMins > presetEndMins) {
//        NSDate *endDate = [NSDate dateWithTimeIntervalSince1970:endSleepTime];
//        NSInteger mins = endDate.hour*60 + endDate.minute;
//        if ((mins < 24*60) && (mins > presetStartMins)) {
//        endSleepTime += D_DAY;
//        }
//
//        }
//        }
//        }
//
//        DBSleepTotalData *total = [[DBSleepTotalData alloc] init];
//        total.quality = 99;
//        total.sleepDuration = inSleepTime;
//        total.lightDuration = lightSleepTime;
//        total.deepDuration = deepSleepTime;
//        total.awakeDuration = soberTime;
//        total.awakeCount = wakeups;
//        total.totalDuration = deepSleepTime + lightSleepTime + soberTime;
//        total.dataType = 0;
//        total.sleepRecordId = arc4random() % 1000000;//取一个随机数当做睡眠id
//        total.sleepDate = endSleepTime;
//
//        [self.sleepTotalDataArray addObject:total];
//        }
//        }